<?php
class StudentResumesController extends AppController {

    public $name = 'StudentResumes';

    public function index(){
        $this->StudentResume->recursive = 0;
        $this->set('studentResumes', $this->paginate());
    }

    public function view($id = null){
        if(!$id){
            $this->Session->setFlash(__('Invalid StudentResume.', true));
            $this->redirect(array('action'=>'index'));
        }
        $this->set('studentResume', $this->StudentResume->read(null, $id));
    }

    /* Add resume record, we need no other parametes but the Student id */
    /* Only called once in StudentResume lifecycle */
    public function add($student_id=null){
        if($student_id){
            $this->loadModel('Student');
            $student = $this->Student->read(null, $student_id);
            if($student['Student']['resume'] == '1'){
                $this->Session->setFlash(__('Resume for you already exists', true));
                $this->redirect(array('action'=>'edit', $this->StudentResume->field('id', 'student_id='.$student_id)));
            }
            $this->data['StudentResume']['student_id'] = $student_id;
            $this->StudentResume->create();
            if($this->StudentResume->save($this->data)){
                $this->Student->id = $student_id;
                $this->Student->saveField('resume', 1);
                $this->Session->setFlash(__('The StudentResume has been saved', true));
                $this->redirect(array('action'=>'edit', $this->StudentResume->getLastInsertId()));
            }else{
                $this->Session->setFlash(__('The StudentResume could not be saved. Please, try again.', true));
            }
        }
        $this->redirect(array('controller'=>'students','action'=>'uhome'));
    }

    public function edit($id = null){
        /* Try to set $this->id to current loggedin student ID */
        if(!$id && !empty($this->user['student'])){
            //App::import('Student');
            $id = $this->StudentResume->field('id', 'StudentResume.student_id='.$this->user['student']['id']);
        }
        if(!$id && empty($this->data)){
            $this->set('status',__('Invalid StudentResume', true));
            $this->redirect(array('controller'=>'students','action'=>'home'));
        }
        if(!empty($this->data)){
            if($this->StudentResume->save($this->data)){
                $this->set('status',__('The StudentResume has been saved', true));
                $this->redirect(array('controller'=>'students','action'=>'home'));
            }else{
                $this->set('status',__('The StudentResume could not be saved. Please, try again.', true));
            }
        }
        if(empty($this->data)){
            $this->data = $this->StudentResume->read(null, $id);
        }
    }

    public function search(){
        if(!empty($this->data)){
            //debug($this->data);
            $searcher = $this->data['StudentResume']['search'];
            $resumes = $this->StudentResume->find('all', array(
                'conditions' => array(
                    'OR' => array(
                        'StudentResume.intention LIKE ' => '%'.$searcher.'%',
                        'StudentResume.awards LIKE ' => '%'.$searcher.'%',
                        'StudentResume.ability LIKE ' => '%'.$searcher.'%'
                    )
                ),
                'contain' => array(
                    'Student'=> array(
                        'StudentMinority',
                        'StudentProvince'
                    )
                )
            ));
            $this->set(compact('searcher', 'resumes'));
        }
    }

}
?>